//=============================================================================
#include "CArea.h"

//=============================================================================
std::vector<CArea> CArea::AreaList;

//=============================================================================
CArea::CArea() {
    AreaSize = 0;
}

//=============================================================================
bool CArea::OnLoad(char* AreaFile) {
    MapList.clear();

    FILE* FileHandle = fopen(AreaFile, "r");

    if(FileHandle == NULL) {
        printf("CArea::OnLoad > Unable to find area file : %s\n", AreaFile);
        return false;
    }

    char TilesetFile[255];

    fscanf(FileHandle, "%s\n", TilesetFile);

    if(Tex_Tileset.OnLoad(TilesetFile) == false) {
        fclose(FileHandle);

        printf("CArea::OnLoad > Unable to load tileset : %s\n", TilesetFile);

        return false;
    }

    fscanf(FileHandle, "%d\n", &AreaSize);

    for(int i = 0;i < AreaSize;i++) {
        for(int j = 0;j < AreaSize;j++) {
            char Buffer[255];

            fscanf(FileHandle, "%s ", Buffer);

            char MapFile[255];
            sprintf(MapFile, "./maps/%s", Buffer);

            CMap tempMap;
            if(tempMap.OnLoad(MapFile) == false) {
                fclose(FileHandle);

                printf("CArea::OnLoad > Unable to load map : %s\n", MapFile);

                return false;
            }

            float Percent = float(j + (i * AreaSize)) / float(AreaSize * AreaSize);

            MapList.push_back(tempMap);
        }
        fscanf(FileHandle, "\n");
    }

    fclose(FileHandle);

    return true;
}

//-----------------------------------------------------------------------------
void CArea::OnRender(int CameraX, int CameraY) {
    Tex_Tileset.Bind();

    int FirstID = 0;

//    int FirstID = -CameraX / WWIDTH;
  //      FirstID = FirstID + ((-CameraY / WHEIGHT) * AreaSize);

    for(int i = 0;i < 4;i++) {
        int ID = FirstID + ((i / 2) * AreaSize) + (i % 2);

        if(ID < 0 || ID >= MapList.size()) continue;

        int X = ID % AreaSize;
        int Y = ID / AreaSize;

        glPushMatrix();
//            glTranslatef(WWIDTH * X, WHEIGHT * Y, 0);

            MapList[ID].OnRender();
        glPopMatrix();
    }
}

//-----------------------------------------------------------------------------
void CArea::OnCleanup() {
}

//=============================================================================
int CArea::GetMapID(int X, int Y) {
//    int MapID = X / WWIDTH;
  //      MapID = MapID + ((Y / WHEIGHT) * AreaSize);

  int MapID = 0;

    if(MapID < 0 || MapID >= MapList.size()) {
        return 0;
    }

    return MapID;
}

//-----------------------------------------------------------------------------
int CArea::GetTileID(int X, int Y) {
    int MapID = GetMapID(X, Y);

    return MapList[MapID].GetTileID(X, Y);
}

//=============================================================================
